﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;
using SDF.Plugin.Misc.SAPAdapter.Core;
using SDF.Plugin.Misc.SAPAdapter.Domain;
using SDF.Plugin.Misc.SAPAdapter.Models;
using SDF.Core.Data;
using SDF.Core;
using SDF.Services.Events;

namespace SDF.Plugin.Misc.SAPAdapter.Services
{
    public class SAPConnectorService : ISAPConnectorService
    {
        private readonly IRepository<SAPInstance> _sapClientRep;
        private readonly IEventPublisher _eventPublisher;

        public SAPConnectorService(
            IRepository<SAPInstance> sapClientRep,
            IEventPublisher eventPublisher)
        {
            this._sapClientRep = sapClientRep;
            this._eventPublisher = eventPublisher;
        }

        public JObject ExecuteSAPFunction(string sapInstance, string functionName, JObject value)
        {
            ISAPOperation sap = new SAPOperation(sapInstance);

            return sap.ExecuteSAPFunction(functionName, value);
        }

        public IEnumerable<FunctionParamsModel> GetFunctionParams(string sapInstance, string functionName)
        {

            ISAPOperation sap = new SAPOperation(sapInstance);

            return sap.GetFunctionParams(functionName);
        }

        public IEnumerable<Dictionary<string, string>> GetTableData(string sapInstance, string tableName, RfcReadTableParamsModel param)
        {
            ISAPOperation sap = new SAPOperation(sapInstance);

            return sap.GetTableData(tableName, param);
        }

        public IEnumerable<TableInfoModel> GetTableInfo(string sapInstance, string tableName)
        {
            ISAPOperation sap = new SAPOperation(sapInstance);

            return sap.GetTableInfo(tableName);
        }

        public bool Ping(string sapInstance)
        {
            ISAPOperation sap = new SAPOperation(sapInstance);

            return sap.Ping();
        }
    }
}